package com.prs.crm.action.project.gantt;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;

import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.project.Project;
import com.prs.crm.domain.project.Task;
import com.prs.crm.service.project.ProjectService;
import com.prs.crm.service.project.TaskService;
import com.prs.crm.vo.project.TaskVo;

public class ProjectGanttAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private ProjectService projectService;
	
	private TaskService taskService;
	
	private PaginationSupport pagination;
	
	private Integer[] projectIds;
	
	private String projectIdStr;
	
	private Project project;
	
	private Collection<TaskVo> tasks;
	
	private TaskVo task;

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public Project getProject() {
		return project;
	}

	public void setProjectIdStr(String projectIdStr) {
		this.projectIdStr = projectIdStr;
	}

	public String getProjectIdStr() {
		return projectIdStr;
	}

	public void setProjectIds(Integer[] projectIds) {
		this.projectIds = projectIds;
	}

	public Integer[] getProjectIds() {
		return projectIds;
	}

	public void setTask(TaskVo task) {
		this.task = task;
	}

	public TaskVo getTask() {
		return task;
	}
	
	
	public void setTasks(Collection<TaskVo> tasks) {
		this.tasks = tasks;
	}

	public Collection<TaskVo> getTasks() {
		return tasks;
	}
	
	
	@Action("projectGantt")
	public String execute()
	{	
		return SUCCESS;
	}
	
	@Action(value="loadTasks",results={@Result(name="success",type="json",params={"root","tasks"})})
	public String loadTasks()
	{
		Collection<Task> taskset = this.getTaskService().findRootTasks(ToolKits.convertStringAryToIntAry(projectIdStr, ","));

		tasks = new ArrayList<TaskVo>();
		for (Task task : taskset)
		{
			TaskVo vo = new TaskVo();
			addChildTask(task,vo);
			tasks.add(vo);
		}
		
		return SUCCESS;
	}
	
	@Action(value="loadTask",results={@Result(name="success",type="json",params={"root","task"})})
	public String loadTask()
	{
		Task rootTask = this.getTaskService().findRootTask(project);
		task = new TaskVo();
		addChildTask(rootTask,task);
		return SUCCESS;
	}
	
	@Action("listGanttProjects")
	public String listGanttProjects()
	{
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getProjectService().listAllProject( pagination));
		return SUCCESS;
	}
	
	@Action(value="mulitiProjectGantt",results={@Result(name="success",location="/project/gantt/mulitiProjectGantt.ftl"),
			@Result(name="input",type="redirect",location="listGanttProjects"),
			@Result(name="error",type="chain",location="listGanttProjects")})
	public String mulitiProjectGantt()
	{
		if(projectIds == null) {
			this.addActionError("请选择要查看的项目");
			return ERROR;
		}
		projectIdStr=null;
		if (this.getProjectIds().length<1)
		{
			return "list";
		}
		else
		{
			for (Integer projectId : projectIds)
			{
				projectIdStr = (projectIdStr==null)?
										projectId.toString():projectIdStr+","+projectId.toString();
			}
			return SUCCESS;
		}
		
	}
	
	private void addChildTask(Task task,TaskVo vo)
	{
		vo.setId(task.getId());
		vo.setName(task.getName());
		vo.setExecutor(task.getExecutor().getResume().getName());
		if (task.getRealStartTime()!=null)
		{
			vo.setStartTime(task.getRealStartTime());
		}
		else
		{
			vo.setStartTime(task.getStartTime());
		}
		if (task.getRealEndTime()!=null)
		{
			if (task.getRealEndTime().compareTo(task.getEndTime())>0)
			{
				vo.setOverTime(true);
			}
			vo.setEndTime(task.getRealEndTime());
		}
		else
		{
			vo.setEndTime(task.getEndTime());
		}
		if (vo.getEndTime().compareTo(vo.getStartTime())<0)
			vo.setStartTime(task.getEndTime());
		if (task.getRate()!=null)
		{
			vo.setPercent(task.getRate());
		}
		else
		{
			vo.setPercent(0);
		}
		if (task.getChildTasks()!=null)
		{
			for (Task childTask : task.getChildTasks())
			{
				TaskVo childVo =  new TaskVo();
				addChildTask(childTask,childVo);
				vo.addChild(childVo);
			}
		}
	}


	

	
}
